import Vue from 'vue'
let fixCacheData = {}

try {
  fixCacheData = uni.getStorageSync('fixCacheData')
} catch (e) {}

const fixCache = {
  state: {
    fixCache: fixCacheData ? fixCacheData : {},
  },
  getters:{
    getFixCacheByWorkOrderId : (state) => (id) => {
      return state.fixCache[id] || null
    }
  },
  mutations: {
    UPDATE_FIX_CACHE: (state, data) => {
      state.fixCache[data.id] = data.cacheData
      uni.setStorageSync('fixCacheData', state.fixCache)
    },
    REMOVE_SINGLE_ORDER_OF_ID : (state, id) =>{
      Vue.delete(state.fixCache,id);
      uni.setStorageSync('fixCacheData', state.fixCache)
    }
  },
  actions:{
    setFixCacheOfWorkOrderId ({commit, state},data) {
      return new Promise((resolve,reject) => {
        if(data.id){
          commit('UPDATE_FIX_CACHE', data)
          resolve()
        }else{
          reject('缓存失败')
        }
      })
    },
    removeCacheOfWorkOrderId({commit, state},id){
      return new Promise((resolve,reject) => {
        if(id && state.fixCache[id] ){
          commit('REMOVE_SINGLE_ORDER_OF_ID', id)
          resolve()
        }else{
          reject()
        }
      })
    }
  }
}

export default fixCache
